const schedule = require('node-schedule');
const sequelize = appRequire('db');
const Sequelize = require('sequelize');
const moment = require('moment');

const userModel = appRequire('models/user');
const artPPTModel = appRequire('models/artPPT');
const miniSendTemplateMsg = appRequire('controllers/wx/sendTemplateMsg');
const { Op } = require("sequelize");

scheduleCronstyle();

//定时任务免费课件
function scheduleCronstyle() {
    schedule.scheduleJob('0 0 1 * * *', async function () {
        console.log('免费课件任务:' + new Date());

        //生成免费课件
        let randPpts = await artPPTModel.findAll({
            where: {
                fileType: "课件",
                sourceFrom: { [Op.ne]: "创意少儿美术馆" },
                status: 1,
                price: 2,
            },
            order: [
                [Sequelize.literal('RAND()')]
            ],
            limit: 15,
        })
        randPpts = Common.clone(randPpts)
        for (let item of randPpts) {
            await artPPTModel.update({
                price: 0
            }, {
                where: {
                    objectId: item.objectId
                },
            })
        }

        //再把昨日的免费课件改价
        let zt = moment().subtract(1, 'day').format('YYYY-MM-DD')
        await artPPTModel.update({
            price: 2
        }, {
            where: {
                price: 0,
                updatedAt: {
                    [Op.gt]: zt + " 00:00:00",
                    [Op.lt]: zt + " 23:59:59"
                },
            },
        })
    });
}


